﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using ISerives;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
using Newtonsoft.Json;

namespace HR.Controllers
{
    public class salaryGrantController : Controller
    {
        private readonly Isalary_grant_Servies igs;
        private readonly Isalary_standard_details_Serives isd;
        private readonly IEsalary_standard_Servies iss;
        private readonly Isalary_grant_details_Serives isdg;

        public salaryGrantController(Isalary_grant_Servies igs, Isalary_standard_details_Serives isd, IEsalary_standard_Servies iss, Isalary_grant_details_Serives isdg)
        {
            this.igs = igs;
            this.isd = isd;
            this.iss = iss;
            this.isdg = isdg;
        }
        public IActionResult Index()
        {
            return View();
        }
        #region 薪酬发放登记


        public IActionResult register_locate()
        {
            return View();
        }

        public async Task<IActionResult> register_list(int currentpage, Esalary_grant esg, int type)
        {
            if (type != 0)
            {
                esg.sgr_id = type;
            }
            FenYe fenye = new FenYe()
            {
                Where = $" check_status=0",
                CurrentPage = currentpage,
                PageSize = 2
            };
            List<Esalary_grant> list = await igs.FenYe(fenye);
            if (fenye.CurrentPage == 0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewData["type"] = esg.sgr_id;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model = list;
            return View();
        }

        public async Task<IActionResult> register_commit(int id, string sid)
        {
            //根据编号查询
            List<Esalary_grant> es = await igs.SelectId(id);
            ViewBag.es = es;
            //根据编号查询名字
            List<Esalary_standard> ss = await iss.ShowId_list(sid);
            ViewBag.ss = ss;
            double num = 0.0;
            foreach (Esalary_standard item in ss)
            {
                num = item.salary_sum;
            }
            ViewData["num"] = num;
            List<salary_standard_details> ssd = await isd.SelectId(sid);
            ViewBag.ssd = ssd;
            return View();
        }
        /// <summary>
        /// 薪酬发放登记添加
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> register_Add(Esalary_grant_details egd)
        {
            Esalary_grant_details e = new Esalary_grant_details()
            {
                salary_grant_id = egd.salary_grant_id,      //: 薪酬发放编号
                human_id = "01",             //: 档案编号
                human_name = egd.human_name,           //: 姓名
                bouns_sum = egd.bouns_sum,            //: 奖励金额
                sale_sum = egd.sale_sum,             // : 销售绩效金额
                deduct_sum = egd.deduct_sum,            //: 应扣金额
                /* salary_standard_sum = egd.salary_standard_sum,  //: 标准薪酬总额*/
                salary_paid_sum = egd.salary_paid_sum,      //: 实发薪酬总额

            };
            if (await isdg.Add(e) > 0)
            {
                return RedirectToAction("check_list");
            }
            else
            {
                return View();
            }
        }

        #endregion

        #region  薪酬发放登记审核 check_list 

        public async Task<IActionResult> check_list(int currentpage)
        {
            FenYe fenye = new FenYe()
            {
                Where = "",
                CurrentPage = currentpage,
                PageSize = 2
            };
            List<Esalary_grant> list = await igs.FenYe(fenye);
            if (fenye.CurrentPage == 0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model = list;
            return View();
        }

        public async Task<IActionResult> check(int id, string sid, string pid)
        {

            //薪酬发放详细信息
            Esalary_grant_details sd = await isdg.Select_id(pid);
            ViewData.Model = sd;
          
            //根据编号查询
            List<Esalary_grant> es = await igs.SelectId(id);
            ViewBag.es = es;
            //根据编号查询名字
            List<Esalary_standard> ss = await iss.ShowId_list(sid);
            ViewBag.ss = ss;
            double num = 0.0;
            foreach (Esalary_standard item in ss)
            {
                num = item.salary_sum;
            }
            ViewData["num"] = num;
            List<salary_standard_details> ssd = await isd.SelectId(sid);
            ViewBag.ssd = ssd;
            return View();
        }

        public async Task<IActionResult> check_success(Esalary_grant_details egd)
        {

            Esalary_grant esg = new Esalary_grant()
            {
                salary_grant_id = egd.salary_grant_id,
                //使用session 找到StartUp类 在ConfigureServices方法中添加一个services.AddSession();
                //使用session 找到StartUp类 在Configure方法中添加 app.UseSession();方法
                checker = HttpContext.Session.GetString("user"),
                check_time = DateTime.Now,
                check_status = 1
            };
            if (await isdg.Upd(egd) > 0)
            {
                await igs.Upd(esg);
                return RedirectToAction("check_list");
            }
            else
            {
                return View();
            }
           

        }
        #endregion

        #region 薪酬发放查询
       
        public IActionResult query_locate()
        {
            return View();
        }
        public async Task<IActionResult> query_list(int currentpage, string id, string sid, Esalary_grant eg)
        {
            if (id!=null)
            {
                eg.salary_grant_id = id;

            }
            if (sid!=null)
            {
                eg.salary_standard_id = sid;
            }
            FenYe fenye = new FenYe()
            {
                Where = $"salary_grant_id like'%{eg.salary_grant_id}%' AND salary_standard_id like '%{eg.salary_standard_id}%'",
                CurrentPage = currentpage,
                PageSize = 2
            };
            List<Esalary_grant> list = await igs.FenYe(fenye);
            if (fenye.CurrentPage == 0)
            {
                fenye.CurrentPage = 1;
            }
            ViewData["cp"] = fenye.CurrentPage;
            ViewData["c"] = fenye.CurrentPage;
            ViewData["bhId"] = eg.salary_grant_id;
            ViewData["sid"] = sid;
            ViewBag.rows = fenye.Rows;
            ViewBag.pages = fenye.Pages;
            ViewBag.pageSize = fenye.PageSize;
            ViewData.Model = list;
            return View();
        }
        public async Task<IActionResult> query(int id, string sid, string pid)
        {
            //薪酬发放详细信息
            Esalary_grant_details sd = await isdg.Select_id(pid);
            ViewData.Model = sd;
            //根据编号查询
            List<Esalary_grant> es = await igs.SelectId(id);
            ViewBag.es = es;
            //根据编号查询名字
            List<Esalary_standard> ss = await iss.ShowId_list(sid);
            ViewBag.ss = ss;
            double num = 0.0;
            foreach (Esalary_standard item in ss)
            {
                num = item.salary_sum;
            }
            ViewData["num"] = num;
            List<salary_standard_details> ssd = await isd.SelectId(sid);
            ViewBag.ssd = ssd;
            return View();
        }
        [HttpGet]
        //薪酬标准详情表报表
        public async Task<IActionResult> strandedDetails(string standardID)
        {
            List<salary_standard_details> list = await isd.SelectId(standardID);
            Dictionary<string, object> di = new Dictionary<string, object>();
            string[] name = new string[list.Count];
            decimal[] va = new decimal[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                name[i] = list[i].item_name;
                va[i] =decimal.Parse(list[i].salary.ToString());
            }
            //di["list"] = list;
            di["name"] = name;
            di["salary"] = va;
            return Content(JsonConvert.SerializeObject(di));
        }

        #endregion
    }
}